﻿namespace OperationFactory
{
    /// <summary>
    /// 抽象产品
    /// 计算类(计算的基本类)
    /// </summary>
    public class Operation
    {
        public double NumberA { get; set; }

        public double NumberB { get; set; }

        /// <summary>
        /// 虚方法：在具体加减乘除计算类中重写
        /// </summary>
        /// <returns>计算结果</returns>
        public virtual double GetResult() 
        {
            double result = 0;
            return result;
        }
    }

    /// <summary>
    /// 具体产品
    /// 加法计算类
    /// </summary>
    public class OperationAdd : Operation 
    {
        public override double GetResult()
        {
            return NumberA+NumberB;
        }
    }

    /// <summary>
    /// 具体产品
    /// 减法计算类
    /// </summary>
    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            return NumberA - NumberB;
        }
    }

    /// <summary>
    /// 具体产品
    /// 乘法计算类
    /// </summary>
    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            return NumberA * NumberB;
        }
    }

    /// <summary>
    /// 具体产品
    /// 除法计算类
    /// </summary>
    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0)
                throw new Exception("除数不能为0.");

            return NumberA / NumberB;
        }
    }
}
